﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Text;
using netDxf;
using netDxf.Blocks;
using netDxf.Collections;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Objects;
using netDxf.Tables;
using netDxf.Units;
using Point = netDxf.Entities.Point;
using Trace = netDxf.Entities.Trace;
using System.Threading;


namespace TestDxfDocument
{
    class class0
    {

        private static void ComplexHatch()
        {
           


            DxfDocument dxf2 = DxfDocument.Load("complexhatch.dxf");
            dxf2.Save("complexhatch2.dxf");
        }

    
        public static void xiaobaishu(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            //图层
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线
            Layer cutting = new Layer("cutting") { Color = AciColor.Red };    //切削层
            Layer nocutting = new Layer("nocutting");    //非切削层
            Layer skcontour = new Layer("skcontour") { Color = AciColor.Cyan };    //sk轮廓层
            Layer skdimensioning = new Layer("skdimensioning");    //sk尺寸线层
            Layer skrefin = new Layer("skrefin") { Color = AciColor.Red };    //sk基准线/内轮廓
            Layer sktext = new Layer("sktext") { Color = AciColor.Blue };  //sk文本
            Layer skhatching = new Layer("skhatching") { Color = AciColor.Blue };    //sk填充
            Layer skadditionlline = new Layer("skadditionlline") { Color = AciColor.Cyan };    //sk附加线
            Layer skviewcontour = new Layer("skviewcontour") { Color = AciColor.Cyan };    //skview轮廓
            Layer skviewdimensioning = new Layer("skviewdimensioning");    //skview尺寸线层
                                                                           //参数
            double pi = Math.PI;
            string path = Convert.ToString(data["path"]);
            //坐标
            Vector2 v0 = new Vector2(5, 10);
            Vector2 v1 = new Vector2(-5, 10);
            Vector2 v2 = new Vector2(10, 10);
            Vector2 v3 = new Vector2(0, 10);

            //Point[] point = new Point[100];

            var line1 = Pus.AddPointsToLwPolyLine(v0, v1);
            dxf.AddEntity(line1);

            var l1 = Pus.RotateByOrigin(90 * pi / 180, line1);
            dxf.AddEntity(l1);

            
            LwPolyline[] linepoarray4 = new LwPolyline[100];
            linepoarray4 = fun.arrayrpolyline(line1, 4);
           
            for (int i = 1; i <= 4; i++)
            {
                //fun.drawBline(dxf, linepoarray4[i], 0.2, contour);
                dxf.AddEntity(linepoarray4[i]);
            };
            

            /*

                        //设置填充样式
                        // HatchPattern pattern = HatchPattern.Load("hatch\\acad.pat", "ESCHER");
                        // HatchPattern pattern = new HatchPattern("SOLID", "Solid fill")// {type = HatchType.Predefined};//实体
                        // HatchPattern pattern = new HatchPattern("LINE", "Parallel horizontal lines");//空的
                        // HatchPattern pattern = new HatchPattern("DOTS", "A series of dots");
                        HatchPattern pattern = new HatchPattern("hatch\\acad.pat", "ESCHER");
                        pattern.Scale = 1.5;
                        pattern.Angle = 30;

                        LwPolyline poly = new LwPolyline();
                        poly.Vertexes.Add(new LwPolylineVertex(-10, -10));
                        poly.Vertexes.Add(new LwPolylineVertex(10, -10));
                        poly.Vertexes.Add(new LwPolylineVertex(10, 10));
                        poly.Vertexes.Add(new LwPolylineVertex(-10, 10));
                        poly.IsClosed = true;//图形需要封闭

                        //给封闭图形填充
                        List<HatchBoundaryPath> boundary = new List<HatchBoundaryPath>
                        {
                           new HatchBoundaryPath(new List<EntityObject> { poly})
                        };
                        Hatch hatch = new Hatch(pattern, boundary, true);

                        dxf.AddEntity(poly);//添加图形
                        dxf.AddEntity(hatch);//添加填充
                        //dxf.Save("complexhatch.dxf");

            */
            /*

                        //填充
                        LwPolyline poly2 = new LwPolyline();
                        poly2.Vertexes.Add(new LwPolylineVertex(v0));
                        poly2.Vertexes.Add(new LwPolylineVertex(v1));
                        poly2.Vertexes.Add(new LwPolylineVertex(v2));
                        poly2.Vertexes.Add(new LwPolylineVertex(v3));
                        poly2.Vertexes.Add(new LwPolylineVertex(v0));
                        poly2.IsClosed = true;
                        String[] htype1 = new String[100];
                        Line[] hline1 = new Line[100];
                        Arc[] harc1 = new Arc[100];
                        int hnumst1 = poly2.Explode().Count;  //记录爆炸后，线段有多少
                        for (int i = 0; i < hnumst1; i++)
                        {
                            htype1[i] = poly2.Explode()[i].Type.ToString();    //将所有的元素记录成一个数组

                            if (htype1[i] == "Line")
                            {
                                hline1[i] = (Line)poly2.Explode()[i];
                            }
                        }
                        fun.hatchsubofline(dxf, htype1, hnumst1, 45, 3, hline1, hatching);



                       */



            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }

        /// <summary>
        /// 图案填充
        /// </summary>
        /// <param name="db">图形数据库</param>
        /// <param name="loopTypes"></param>
        /// <param name="patternName">图案名称</param>
        /// <param name="scale">填充比例</param>
        /// <param name="degree">旋转角度</param>
        /// <param name="entid">边界图形的ObjectId</param>
        /// <returns></returns>
        /// 

        /*
        public static ObjectId HatchEnity(this Database db, List<HatchLoopTypes> loopTypes, string patternName, double scale, double degree, params ObjectId[] entid) // 一个方法只能传递一个可变参数 且需要放在最后
        {
            ObjectId hatchId = ObjectId.Null;
            using (Transaction trans = db.TransactionManager.StartTransaction())
            {
                // 声明一个图案填充对象
                Hatch hatch = new Hatch();
                // 设置填充比例
                hatch.PatternScale = scale;
                // 设置填充类型和图案名称
                hatch.SetHatchPattern(HatchPatternType.PreDefined, "ANGLE");
                // 加入图形数据库
                BlockTable bt = (BlockTable)trans.GetObject(db.BlockTableId, OpenMode.ForRead);
                BlockTableRecord btr = (BlockTableRecord)trans.GetObject(bt[BlockTableRecord.ModelSpace], OpenMode.ForWrite);
                hatchId = btr.AppendEntity(hatch);
                trans.AddNewlyCreatedDBObject(hatch, true);

                // 设置填充角度
                hatch.PatternAngle = degree;
                // 设置关联
                hatch.Associative = true;
                // 设置边界图形和填充方式


                ObjectIdCollection obIds = new ObjectIdCollection();
                // 依次添加图形填充样式
                for (int i = 0; i < entid.Length; i++)
                {
                    obIds.Clear();
                    obIds.Add(entid[i]);
                    hatch.AppendLoop(loopTypes[i], obIds);
                }


                // 计算填充并显示
                hatch.EvaluateHatch(true);
                // 提交事务
                trans.Commit();
            }
            return hatchId;
        }

        */
        public static void TriangularInsert(Dictionary<string, Object> data)    //正三边形刀片，适用于T形刀片
        {
            DxfDocument dxf = new DxfDocument();




            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer dimensioning = new Layer("dimensioning");    //尺寸层
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓
            Layer center = new Layer("center") { Color = AciColor.Yellow };    //中心线
            Layer textlayer = new Layer("textlayer") { Color = AciColor.Blue };    //文本层
            Layer hatching = new Layer("hatching") { Color = AciColor.Blue };    //填充层
            Layer additionalline = new Layer("additionalline") { Color = AciColor.Cyan };    //附加线

            double tic = Convert.ToDouble(data["ic"]);
            double scale = Convert.ToDouble(data["scale"]); //放大系数
            if (scale == 0 && tic < 9)
            {
                scale = 3.0;
            }
            else if (scale == 0 && tic < 20)
            {
                scale = 2.5;
            }
            else if (scale == 0)
            {
                scale = 2.0;
            }



            double pi = Math.PI;
            double DIM = Convert.ToDouble(data["dim"]);
            double ic = Convert.ToDouble(data["ic"]) * scale;
            double s = Convert.ToDouble(data["s"]) * scale;
            double d1 = Convert.ToDouble(data["d1"]) * scale;
            double re = Convert.ToDouble(data["re"]) * scale;
            double epsr = Convert.ToDouble(data["epsr"]) * pi / 180;
            double an = Convert.ToDouble(data["an"]) * pi / 180;
            // int ifs = (int)Convert.ToDecimal(data["ifs"]);
            int cb = (int)Convert.ToDecimal(data["cb"]);
            string path = Convert.ToString(data["path"]);
            double le = 2 * ic * Math.Cos(pi / 6);
            double ictc = Convert.ToDouble(data["ictc"]);
            double stc = Convert.ToDouble(data["stc"]);
            //double d1tc = Convert.ToDouble(data["d1tc"]);
            double dxc = 0.8;

            Vector2 p1 = new Vector2(0, 0);
            Vector2 p0 = fun.Polar(p1, pi / 6, ic);
            Vector2 p2 = fun.Polar(p1, 0, le);
            Vector2 p3 = fun.Polar(p1, pi / 3, le);
            Vector2 p1p = fun.Polar(p1, pi / 6, 2 * re);
            Vector2 p2p = fun.Polar(p2, 5 * pi / 6, 2 * re);
            Vector2 p3p = fun.Polar(p3, -0.5 * pi, 2 * re);
            Vector2 p4 = fun.Polar(p1p, 5 * pi / 6, re);
            Vector2 p5 = fun.Polar(p1p, -0.5 * pi, re);
            Vector2 p6 = fun.Polar(p2p, -0.5 * pi, re);
            Vector2 p7 = fun.Polar(p2p, pi / 6, re);
            Vector2 p8 = fun.Polar(p3p, pi / 6, re);
            Vector2 p9 = fun.Polar(p3p, 5 * pi / 6, re);

            Vector2 p20 = fun.Polar(p0, 0, 2 * ic);
            Vector2 p21 = new Vector2(p20.X, p3p.Y + re);

            Vector2 p22 = fun.Polar(p21, pi + an, s / Math.Cos(an));
            Vector2 p24 = new Vector2(p20.X, p1.Y);
            Vector2 p23 = fun.Polar(p24, pi - an, s / Math.Cos(an));

            Vector2 v1 = fun.Polar(p0, -150 * pi / 180, ic / 2 / Math.Cos(60 * pi / 180) * dxc);
            Vector2 v2 = fun.Polar(p0, -30 * pi / 180, ic / 2 / Math.Cos(60 * pi / 180) * dxc);
            Vector2 v3 = fun.Polar(p0, 90 * pi / 180, ic / 2 / Math.Cos(60 * pi / 180) * dxc);


            var a1 = fun.Polar(p1, fun.CalAngle(p1, p2), le - 2 * re * Math.Cos(60 * pi / 180) - 0.8 * scale);
            var a2 = fun.Inters(a1, fun.Polar(a1, 0, 1), p2, p3);
            var line34 = new Line(a1, a2);
            fun.drawBline(dxf, line34, 0.2, contour);


            if (cb == 1 || cb == 2)
            {


                Line line15 = new Line(v1, v2);
                line15.Lineweight = Lineweight.W25;
                line15.Layer = additionalline;
                dxf.AddEntity(line15);
                Line line16 = new Line(v2, v3);
                line16.Lineweight = Lineweight.W25;
                line16.Layer = additionalline;
                dxf.AddEntity(line16);
                Line line17 = new Line(v1, v3);
                line17.Lineweight = Lineweight.W25;
                line17.Layer = additionalline;
                dxf.AddEntity(line17);

            }


            insert.inserthole(dxf, p0, v2, v3, p20, p21, scale, data);

            Arc arc3 = fun.DrawArc(p8, p3p, p9);

            LwPolyline poly11 = new LwPolyline();
            poly11.Vertexes.Add(new LwPolylineVertex(p4));
            poly11.Vertexes[0].Bulge = Math.Tan((pi - epsr) / 4);
            poly11.Vertexes.Add(new LwPolylineVertex(p5));
            poly11.Vertexes.Add(new LwPolylineVertex(p6));
            poly11.Vertexes[2].Bulge = Math.Tan((pi - epsr) / 4);
            poly11.Vertexes.Add(new LwPolylineVertex(p7));
            poly11.Vertexes.Add(new LwPolylineVertex(p8));
            poly11.Vertexes[4].Bulge = Math.Tan((pi - epsr) / 4);
            poly11.Vertexes.Add(new LwPolylineVertex(p9));
            poly11.IsClosed = true;
            fun.PolyFill(dxf, poly11, 0.5, contour);

            Line line12 = new Line(p1, p2);
            line12.Lineweight = Lineweight.W25;
            line12.Layer = additionalline;
            dxf.AddEntity(line12);
            Line line13 = new Line(p2, p3);
            line13.Lineweight = Lineweight.W25;
            line13.Layer = additionalline;
            dxf.AddEntity(line13);
            Line line14 = new Line(p1, p3);
            line14.Lineweight = Lineweight.W25;
            line14.Layer = additionalline;
            dxf.AddEntity(line14);



            Line cenline1 = new Line(fun.Polar(p0, pi, ic / 2 * 1.5), fun.Polar(p0, 0, ic / 2 * 1.5));
            Line cenline2 = new Line(fun.Polar(p0, 0.5 * pi, ic * 1.1), fun.Polar(p0, -0.5 * pi, ic / 2 * 1.5));
            Line cenline3 = new Line(fun.Polar(p20, 0, 3), fun.Polar(p20, pi, s + 3));
            Circle dotcircle1 = new Circle(p0, ic / 2);
            cenline1.Layer = center;
            cenline2.Layer = center;
            cenline3.Layer = center;
            dotcircle1.Layer = refin;
            cenline1.Linetype = Linetype.Center;
            cenline2.Linetype = Linetype.Center;
            cenline3.Linetype = Linetype.Center;
            dotcircle1.Linetype = Linetype.DashDot;
            dxf.AddEntity(cenline1);
            dxf.AddEntity(cenline2);
            dxf.AddEntity(cenline3);
            dxf.AddEntity(dotcircle1);

            fun.jz(dxf, fun.Polar(fun.Polar(p0, 0, ic / 2), -90 * pi / 180, 25), 0, 3, "A");

            DimensionStyle dimensionStyle = new DimensionStyle("sd");
            dimensionStyle.ArrowSize = 1.6;
            dimensionStyle.DimArrow1 = DimensionArrowhead.Closedfilled;
            dimensionStyle.DimArrow2 = DimensionArrowhead.Closedfilled;
            dimensionStyle.TextHeight = 1.2;
            double offset = 10;
            //标注
            if (DIM == 0)
            {
                LinearDimension dim1 = new LinearDimension(new Line(fun.Polar(p0, pi, ic / 2), fun.Polar(p0, 0, ic / 2)), offset * 2.5, 180, dimensionStyle);
                dim1.UserText = "IC";
                dxf.AddEntity(dim1);

                LinearDimension dim2 = new LinearDimension(new Line(p23, p24), offset, 180, dimensionStyle);
                dim2.UserText = "S";
                dxf.AddEntity(dim2);

                if (re / scale != 0.001)
                {
                    RadialDimension dimrad1 = new RadialDimension(arc3, 10, offset, dimensionStyle);//DimensionStyle.Default);
                    dimrad1.UserText = "RE";
                    dxf.AddEntity(dimrad1);
                }

                LinearDimension dim5 = new LinearDimension(new Line(p1, p2), offset * 2.25, 180, dimensionStyle);
                dim5.UserText = "LE";
                dxf.AddEntity(dim5);

                if (an != 0)
                {/*
                    Angular2LineDimension dimang2 = new Angular2LineDimension(new Line(p23, fun.Polar(p23, pi - an, 1)), new Line(p24, fun.Polar(p24, pi, 1)), s + offset, dimensionStyle);
                    dimang2.UserText = "AN=" + an * 180 / pi + "°";
                    dxf.AddEntity(dimang2);*/
                    fun.jdbz2(dxf, new Line(p23, fun.Polar(p23, pi - an, 1)), new Line(p24, fun.Polar(p24, pi, 1)), s + offset, 1.2, 1.6, 0.5, "AN=" + an * 180 / pi + "°", "", "");

                }
            }
            else
            {
                if (ictc == 0)
                {
                    LinearDimension dim1 = new LinearDimension(new Line(fun.Polar(p0, pi, ic / 2), fun.Polar(p0, 0, ic / 2)), offset * 2.5, 180, dimensionStyle);
                    dim1.UserText = String.Format("IC={0}", ic / scale);
                    dxf.AddEntity(dim1);
                }
                if (ictc != 0)
                {
                    LinearDimension dim1 = new LinearDimension(new Line(fun.Polar(p0, pi, ic / 2), fun.Polar(p0, 0, ic / 2)), offset * 2.5, 180, dimensionStyle);
                    dim1.UserText = String.Format("IC={0}±{1}", ic / scale, ictc);
                    dxf.AddEntity(dim1);
                }
                if (stc == 0)
                {
                    LinearDimension dim2 = new LinearDimension(new Line(p23, p24), offset, 180, dimensionStyle);
                    dim2.UserText = String.Format("S={0}", s / scale);
                    dxf.AddEntity(dim2);
                }
                if (stc != 0)
                {
                    LinearDimension dim2 = new LinearDimension(new Line(p23, p24), offset, 180, dimensionStyle);
                    dim2.UserText = String.Format("S={0}±{1}", s / scale, stc);
                    dxf.AddEntity(dim2);
                }


                if (re / scale != 0.001)
                {
                    RadialDimension dimrad1 = new RadialDimension(arc3, 10, offset, dimensionStyle);//DimensionStyle.Default);
                    dimrad1.UserText = String.Format("RE={0}", re / scale);
                    dxf.AddEntity(dimrad1);
                }
                LinearDimension dim5 = new LinearDimension(new Line(p1, p2), offset * 2.25, 180, dimensionStyle);
                dim5.UserText = String.Format("LE={0}", Math.Round(le / scale, 2));
                dxf.AddEntity(dim5);

                if (an != 0)
                {
                    /*Angular2LineDimension dimang2 = new Angular2LineDimension(new Line(p23, fun.Polar(p23, pi - an, 1)), new Line(p24, fun.Polar(p24, pi, 1)), s + offset, dimensionStyle);
                    dimang2.UserText = "AN=" + an * 180 / pi + "°";
                    dxf.AddEntity(dimang2);*/
                    fun.jdbz2(dxf, new Line(p23, fun.Polar(p23, pi - an, 1)), new Line(p24, fun.Polar(p24, pi, 1)), s + offset, 1.2, 1.6, 0.5, "AN=" + an * 180 / pi + "°", "", "");


                }
            }

            double isframe = Convert.ToDouble(data["isframe"]);
            if (isframe == 1)
            {
                Dictionary<string, Vector2> position = Frame.frameA4Y(dxf, new Vector2(p0.X - 90, p0.Y + 120), data);
                fun.writetext(dxf, scale + ":1", position["比例"], 3.5, 0, 0.6, "C");
            }

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }


    }
}
